<template>
  <!-- <div class="card-box">


    </div> -->
  <div>
    <!-- <div class="heards-box">
      <t-tabs v-model="tabValue" @change="changeVlaue">
        <t-tab-panel v-for="item in tabList" :key="item.value" :value="item.value" :label="item.title"> </t-tab-panel>
      </t-tabs>
    </div> -->
    <t-card class="dashboard-detail-card">
      <template #header>
        <t-tabs v-model="tabValue" @change="changeVlaue">
          <t-tab-panel v-for="item in tabList" :key="item.value" :value="item.value" :label="item.title"> </t-tab-panel>
        </t-tabs>
      </template>
      <div class="content-box">
        <t-row style="margin: 0; min-width: 740px">
          <t-col :span="7" style="margin-bottom: 0">
            <div class="col-left-box">
              <button class="hw-btn-style hw-btn-style-page" @click="addaddExhibits(tabValue)" v-if="tabValue !== 3">
                新建展品
              </button>
              <button class="hw-btn-style hw-btn-style-page" @click="recovery()" v-else>恢复展品</button>
              <t-upload
                v-if="tabValue == 2"
                v-loading="uploadLoading"
                ref="uploadRef1"
                v-model="files1"
                accept="*,.zip"
                method="POST"
                name="formFile"
                :headers="heards"
                :action="uploadUrlEx"
                :size-limit="{ size: 50, unit: 'MB' }"
                :max="1"
                :allow-upload-duplicate-file="true"
                @success="handleSuccess"
                @selectChange="selectChange"
                @fail="onFail"
              >
                <button class="hw-btn-style2">
                  <span>导入展品</span>
                </button>
              </t-upload>
              <a
                v-if="tabValue == 2"
                class="load-item"
                href="//static.jiyi.show/exhibitsZip/%E7%A7%81%E6%9C%89%E5%BA%93%E5%AF%BC%E5%85%A5%E6%A8%A1%E6%9D%BF%EF%BC%88%E4%B8%80%E6%99%AE%E6%A0%BC%E5%BC%8F%EF%BC%89.zip"
                >导入模板下载</a
              >
            </div>
          </t-col>
          <t-col :span="5" style="margin-bottom: 0">
            <div class="hw-box-style-flex hw-box-style-flex-end" style="width: 100%">
              <div class="hw-box-style-flex w-box-style-flex-end">
                <t-input @Enter="onEnter" placeholder="请输入搜索内容" style="width: 100%" v-model="parmas.value">
                  <template #suffix-icon>
                    <SearchIcon @click="onEnter" />
                  </template>
                </t-input>
              </div>
              <div class="contents-btn">
                <button class="hw-btn-style2" @click="visible = true">
                  <t-icon name="filter" />
                  <span style="margin-left: 6px">筛选</span>
                </button>
              </div>
            </div>
          </t-col>
        </t-row>
        <div class="table-box">
          <t-table
            :headerAffixedTop="true"
            maxHeight="100%"
            :loading="loading"
            row-key="id"
            :data="tabDatas"
            :columns="columns"
            :selected-row-keys="selectedRowKeys"
            select-on-row-click
            @select-change="rehandleSelectChange"
            :table-content-width="tableLayout === 'fixed' ? undefined : '1200px'"
            :resizable="false"
          >
            <template #coverUrl="{ row }">
              <t-image
                v-if="row.coverUrl"
                :src="$filters.getLinkUrl(row.coverUrl, webUrls)"
                fit="cover"
                position="center"
                class="avator-box"
              />
              <div class="avator-box avator-box-null" v-else></div>
            </template>
            <template #typeId="{ row }">
              <t-select :disabled="true" :borderless="true" v-model="row.typeId" placeholder="未添加类型">
                <t-option v-for="item in datas.select3" :key="item.id" :label="item.text" :value="item.id" />
              </t-select>
            </template>
            <template #ageId="{ row }">
              <t-cascader
                :disabled="true"
                :borderless="true"
                placeholder="未添加年代"
                v-model="row.ageId"
                :options="datas.select4"
                :keys="{
                  value: 'id',
                  children: 'children',
                  label: 'text',
                }"
                clearable
                @change="onChange"
              />
            </template>
            <template #SpecificAge="{ row }">
              <div class="text-ov-hid-style">
                {{ row.SpecificAge ? row.SpecificAge : '--' }}
              </div>
            </template>
            <template #size="{ row }">
              <div class="text-ov-hid-style">{{ row.size ? row.size : '--' }}</div>
            </template>
            <template #levelId="{ row }">
              <t-select :disabled="true" :borderless="true" v-model="row.levelId" placeholder="未添加级别">
                <t-option v-for="item in datas.select1" :key="item.id" :label="item.text" :value="item.id" />
              </t-select>
            </template>
            <template #publishStatus="{ row }">
              <div
                style="display: flex"
                class="hw-box-style-flex-r-center"
                :class="{
                  'style-publish1': row.publishStatus == 1,
                  'style-publish2': row.publishStatus == 2,
                  'style-publish3': row.publishStatus == 3,
                  'style-publish4': row.publishStatus == 4,
                  'style-publish4': row.publishStatus == 5,
                }"
              >
                <span
                  class="dot-care"
                  :class="{
                    'dot-care-publish1': row.publishStatus == 1,
                    'dot-care-publish2': row.publishStatus == 2,
                    'dot-care-publish3': row.publishStatus == 3,
                    'dot-care-publish4': row.publishStatus == 4,
                    'dot-care-publish4': row.publishStatus == 5,
                  }"
                ></span>
                <span v-if="row.publishStatus === 1">未发布</span>
                <span v-if="row.publishStatus === 2">审核中</span>
                <span v-if="row.publishStatus === 3">已发布</span>
                <span v-if="row.publishStatus === 4">已撤回</span>
                <span v-if="row.publishStatus === 5">已退回</span>
              </div>
            </template>
            <template #operation="{ row }">
              <div class="operation-box" v-if="tabValue !== 3">
                <span class="hw-click-cursor" @click="edit(row.id)">编辑</span>
                <t-popup :overlay-style="{ width: '160px' }" destroy-on-close trigger="click" placement="bottom">
                  <template #content>
                    <div class="pop-box">
                      <div class="pop-item hw-click-cursor" v-if="tabValue == 2" @click="moveInfo(row.id)">
                        移动到公有库
                      </div>
                      <div class="pop-item hw-click-cursor" v-if="tabValue == 1" @click="moveInfo(row.id)">
                        移动到私有库
                      </div>
                      <div class="pop-item hw-click-cursor" @click="delRowId(row.id)">移动到回收站</div>
                    </div>
                  </template>
                  <span class="hw-click-cursor">移动 </span>
                </t-popup>
                <t-popup :overlay-style="{ width: '160px' }" destroy-on-close trigger="hover" placement="bottom">
                  <template #content>
                    <div class="pop-box">
                      <div
                        class="pop-item hw-click-cursor"
                        v-if="row.publishStatus == 3 || row.publishStatus == 2"
                        @click="withdrawn(row.id)"
                      >
                        撤回
                      </div>
                      <div class="pop-item hw-click-cursor" @click="publish(row.id)" v-else-if="tabValue == 1">
                        发布
                      </div>

                      <div class="pop-item hw-click-cursor" @click="delRowId(row.id)">删除展品</div>
                      <div class="pop-item hw-click-cursor" @click="LookRowId(row.id)">门户预览</div>
                    </div>
                  </template>
                  <span class="hw-click-cursor"
                    >更多
                    <t-icon name="chevron-down" style="color: #d2ad77" />
                  </span>
                </t-popup>
              </div>
              <div v-else class="operation-box">
                <span class="hw-click-cursor" @click="recovery(row.id)">恢复 </span>
              </div>
            </template>
            <template #empty v-if="tabValue !== 3">
              <div class="hw-flex-style-c-c-c hw-box-style-flex-clm null-box-style">
                <img class="null-img-1" src="../../../assets/img/null1.png" alt="" />
                <span class="null-title-1">暂无展品</span>
                <span class="null-title-2">快去新建展品吧</span>
                <button class="hw-btn-style hw-btn-style-page" @click="addaddExhibits(tabValue)">新建展品</button>
              </div>
            </template>
          </t-table>
          <t-row style="margin-top: 24px">
            <t-col :span="12" v-if="total !== 0">
              <t-pagination
                v-model="parmas.pageNo"
                v-model:pageSize="parmas.pageSize"
                :total="total"
                show-sizer
                :show-page-size="true"
                :page-size-options="pageSizeOptions"
                @change="onChange"
                @page-size-change="onPageSizeChange"
                @current-change="onCurrentChange"
              />
            </t-col>
          </t-row>
        </div>
      </div>
      <t-drawer v-model:visible="visible" header="筛选" :on-confirm="onClickConfirm" :close-btn="true" size="400px">
        <t-row>
          <t-col :span="12">材质类型</t-col>
          <t-col :span="12">
            <t-select v-model="typeId">
              <t-option v-for="item in datas.select3" :key="item.id" :label="item.text" :value="item.id" />
            </t-select>
          </t-col>
          <t-col :span="12">所属年代</t-col>
          <t-col :span="12">
            <t-cascader
              v-model="ageId"
              :options="datas.select4"
              :keys="{
                value: 'id',
                children: 'children',
                label: 'text',
              }"
              clearable
              @change="onChange"
              check-strictly
            />
          </t-col>
          <t-col :span="12">展品级别 </t-col>
          <t-col :span="12">
            <div class="select-box">
              <div
                class="hw-click-cursor"
                :class="{ 'select-box-item': true, 'select-box-item-active': levelId == item.id }"
                v-for="item in datas.select1"
                :key="item.id"
                @click="levelId = item.id"
              >
                {{ item.text }}
              </div>
            </div>
            <!-- <t-select v-model="parmas.levelId">
              <t-option v-for="item in datas.select1" :key="item.id" :label="item.text" :value="item.id" />
            </t-select> -->
          </t-col>
        </t-row>
        <template #footer>
          <div class="footer-box">
            <t-button variant="outline" @click="onReset"> 清空 </t-button>
            <div>
              <t-button variant="outline" @click="visible = false"> 取消 </t-button>
              <t-button @click="onSubmit">筛选</t-button>
            </div>
          </div>
        </template>
      </t-drawer>
      <messageModel
        submitText="删除"
        title="提示"
        :visible="visible1"
        problem="确定删除此展品？"
        message="删除后可在回收站找回"
        @closeClick="closeClick"
        @submitClick="submitClick"
      />
    </t-card>
  </div>
</template>
<script setup lang="ts">
// @ts-nocheck
import { doorUrl } from '@/utils/minxin';
import { heards, uploadUrlEx } from '@/utils/request/upload';
import { SearchIcon } from 'tdesign-icons-vue-next';
import { MessagePlugin } from 'tdesign-vue-next';
import messageModel from '@/components/messageModel/index.vue';
import { treeList } from '@/api/user/user';
import {
  enterpriseList,
  exhibitDisable,
  exhibitRecovery,
  exhibitMove,
  exhibitPublish,
  exhibitWithdrawn,
} from '@/api/exhibit/exhibit';
import { LockOnIcon } from 'tdesign-icons-vue-next';
import { ref, computed, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { tabList, column, listdata, tableType, delColumn, column2 } from '../data';
import { useNotificationStore } from '@/store';

const store = useNotificationStore();
const pageSizeOptions = [
  { label: '每页 10 条', value: 10 },
  { label: '每页 30 条', value: 20 },
  { label: '每页 50 条', value: 50 },
  { label: '每页 100 条', value: 100 },
];
const webUrls: string = '?imageView2/1/w/48/h/48/format/webp';
const loading = ref(false);
const uploadLoading = ref(false);
const router = useRouter();
const files1 = ref([]);
const delId = ref(0);
const tabValue = ref(1);
const emit = defineEmits(['addExhibit']);
const total = ref(0);
const visible = ref(false);
const visible1 = ref(false);
const tableLayout = ref('fixed');
const emptyData = ref(false);
const typeId = ref(0);
const ageId = ref(0);
const levelId = ref(0);
const data = reactive({
  tabletype: tableType,
});
const tabDatas = ref([]);
const parmas = reactive({
  value: '',
  pageNo: 1,
  pageSize: 20,
  menu: 1,
  typeId: 0,
  ageId: 0,
  levelId: 0,
  sourceId: 0,
});
const datas = reactive({
  select1: [], //级别
  select2: [], //来源
  select3: [], //类型
  select4: [], //年代
});

const initSelect = async () => {
  //初始化所有下拉选项
  let res = await treeList(371994471206981); //级别
  let res1 = await treeList(371996102197317); //来源
  let res2 = await treeList(371999507447877); //类型
  let res3 = await treeList(372051821408325); //年代
  datas.select1 = res.data;
  datas.select2 = res1.data;
  datas.select3 = res2.data;
  datas.select4 = res3.data;
};
const init = async () => {
  try {
    loading.value = true;
    let { data } = await enterpriseList(parmas);
    total.value = data.total;
    tabDatas.value = data.data;
    loading.value = false;
  } catch {
    loading.value = false;
  }
};
init();
initSelect();
const columns = computed(() => {
  parmas.menu = tabValue.value;
  if (tabValue.value == 3) {
    return delColumn;
  } else if (tabValue.value == 2) {
    return column2;
  } else {
    return column;
  }
});
const selectedRowKeys = ref([]);
const onPageSizeChange = (size) => {
  init();
};
const LookRowId = (id: number) => {
  let urlHref = doorUrl + 'portal/visitExhibitsInfo?showpieceid=' + id;
  window.open(urlHref, '_blank');
};
const onEnter = () => {
  parmas.pageNo = 1;
  init();
};
const onCurrentChange = (index, pageInfo) => {
  //分页事件
  init();
};
const changeVlaue = (e: any) => {
  parmas.menu = e;
  init();
};
const publish = (e: number) => {
  //发布展品
  exhibitPublish(e).then((res) => {
    if (res.statusCode == 200) {
      MessagePlugin.success(`发布成功`);
      init();
    } else {
      MessagePlugin.error(res.errors);
    }
  });
};
const recovery = (e?: number) => {
  let str = '';
  if (e) {
    str = str + e;
  } else {
    if (selectedRowKeys.value.length == 0) {
      MessagePlugin.error(`请选择要恢复的数据`);
      return;
    }
    selectedRowKeys.value.forEach((element) => {
      str = str + element + ',';
    });
    console.log(str);
  }
  exhibitRecovery(str).then((res: any) => {
    if (res.statusCode == 200) {
      MessagePlugin.success(`恢复成功`);
      if (e) {
        selectedRowKeys.value.forEach((element, index) => {
          if (element.id == e) {
            selectedRowKeys.value.splice(index, 1);
          }
        });
      } else {
        selectedRowKeys.value = [];
      }

      init();
    } else {
      MessagePlugin.error(res.errors);
    }
  });
};
const withdrawn = (e: number) => {
  exhibitWithdrawn(e).then((res: any) => {
    if (res.statusCode == 200) {
      MessagePlugin.success(`撤回成功`);
      init();
    } else {
      MessagePlugin.error(res.errors);
    }
  });
};

const moveInfo = (e: number) => {
  //移动展品
  exhibitMove(e).then((res: any) => {
    if (res.statusCode == 200) {
      MessagePlugin.success(`移动成功`);
      init();
    } else {
      // MessagePlugin.error(res.errors);
    }
  });
};
const onChange = (pageInfo) => {};
const addaddExhibits = (e: number) => {
  //新建展品
  router.push({ path: '/Exhibits/addExhibitLibrary', query: { type: e } });
};
const edit = (e: number) => {
  //编辑展品
  router.push({ path: '/Exhibits/addExhibitLibrary', query: { id: e } });
};
const rehandleSelectChange = (value, { selectedRowData }) => {
  //恢复列表选项
  selectedRowKeys.value = value;
};
const closeClick = () => {
  visible1.value = false;
};
const submitClick = () => {
  //删除展品
  visible1.value = false;
  exhibitDisable(delId.value).then((res: any) => {
    if (res.statusCode == 200) {
      MessagePlugin.success(`删除成功`);
      init();
    }
  });
};
const handleSuccess = () => {
  uploadLoading.value = false;
  files1.value = [];
  MessagePlugin.success(`上传成功,导入结果将会通过消息进行通知`);
  store.getList();
};
const selectChange = () => {
  uploadLoading.value = true;
};
const onFail = (err: any) => {
  console.log(err);
  files1.value = [];
  uploadLoading.value = false;
};
const onReset = () => {
  parmas.typeId = 0;
  parmas.ageId = 0;
  parmas.levelId = 0;
  typeId.value = 0;
  ageId.value = 0;
  levelId.value = 0;
};
const onSubmit = () => {
  parmas.typeId = typeId.value;
  parmas.ageId = ageId.value;
  parmas.levelId = levelId.value;
  parmas.pageNo = 1;
  visible.value = false;
  init();
};
const onClickConfirm = () => {};
const delRowId = (e: number) => {
  delId.value = e;
  visible1.value = true;
};
</script>

<style lang="less" scoped>
@import '../index.less';
:deep(.t-card) {
  height: calc(100vh - 170px);
}
.content-box {
  display: flex;
  flex-direction: column;
  height: calc(100vh - 282px) !important;

  :deep(.t-input.t-is-disabled) {
    background: #fff;
  }
  :deep(.t-input.t-is-disabled .t-input__inner) {
    color: rgba(0, 0, 0, 0.9);
  }
  :deep(.t-select.t-select-input--borderless .t-select__right-icon) {
    display: none;
  }
  :deep(.t-cascader .t-fake-arrow) {
    display: none;
  }
  :deep(.t-select-input--borderless .t-input) {
    border: none;
  }
}
.contents-btn {
  display: flex;
  justify-content: flex-end;
}
.w-btn-style2 {
  margin: 0;
}
.content-box {
  // min-width: 1180px;
  height: 100%;
}
.hw-btn-style {
  height: 32px;
}
.hw-btn-style2 {
  margin-left: 16px;
  height: 32px;
}
:deep(.t-table__header--fixed:not(.t-table__header--multiple) > tr > th) {
  background: #fff;
}
.t-table {
  border: 1px solid #e7e7e7;
  // flex: 1;

  max-height: calc(100% - 72px);
  // overflow: scroll;
}
:deep(.content-box .t-input.t-is-disabled .t-input__inner) {
  font-size: 16px;
}
.t-col {
  margin-bottom: 16px;
  margin-top: 0 !important;
}
.t-row {
  margin-top: 0;
}
:deep(.t-select__wrap) {
  margin-bottom: 8px;
}
:deep(.t-input__wrap) {
  margin-bottom: 0px;
}
:deep(.t-select__wrap) {
  margin-bottom: 0px;
}
.table-box {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  margin-bottom: 0px;
  flex: 1;
  max-height: calc(100% - 32px);
}
.col-left-box {
  display: flex;
}
.load-item {
  line-height: 32px;
  font-size: 14px;
  margin-left: 16px;
}
.load-item:hover {
  color: #d2ad77;
}
:deep(.t-upload__tips) {
  display: none;
}
:deep(.t-upload__single-display-text) {
  display: none;
}
:deep(.t-upload .t-icon-loading) {
  color: #d2ad77;
}
:deep(.t-loading__gradient-conic) {
  background: conic-gradient(from 90deg at 50% 50%, rgba(0, 82, 217, 0) 0deg, #d2ad77 360deg) !important;
}
</style>
